package com.jushu.storbox.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.util.DirectoryUtil;
import com.jushu.storbox.util.OnImageDownload;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.util.image.IconVerification;
import com.jushu.storbox.util.image.ImageDownloader;
import com.jushu.storbox.view.viewImpl.MyImageView;

public class FileBrowserAllFileAdapter extends BaseAdapter {
	private List<File> files = null;

	private ArrayList<String> selectedFiles = null;

	public static List<Boolean> isSelected;

	private Context context;

	private ListView listView;

	private ImageDownloader mDownloader;

	private ViewHolder holder;

	public ArrayList<String> getSelectedFiles() {
		return selectedFiles;
	}

	public ViewHolder getHolder() {
		return holder;
	}

	public FileBrowserAllFileAdapter(List<File> files, ListView listView, Context context) {
		this.files = files;
		this.context = context;
		selectedFiles = new ArrayList<String>();
		this.listView = listView;
		getIsSelected();
	}

	public void getIsSelected() {
		isSelected = new ArrayList<Boolean>();
		for (int i = 0; i < files.size(); i++) {
			isSelected.add(i, false);
		}
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public File getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.uplod_file_listview_item, null);
			holder = new ViewHolder();
			holder.iv = (MyImageView) convertView.findViewById(R.id.iv_icon);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.file_size = (TextView) convertView.findViewById(R.id.upload_file_size);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.is_item_select);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.item = new File(this.getItem(position).getPath());
		holder.tv_name.setText(holder.item.getName());
		holder.file_size.setText(PublicTools.changeBKM(String.valueOf(holder.item.length())));
		if (holder.item.isDirectory()) {
			holder.iv.setImageResource(R.mipmap.folder_im);
			holder.checkBox.setVisibility(View.GONE);
			holder.file_size.setText("");
		} else {
			holder.checkBox.setVisibility(View.VISIBLE);
			holder.checkBox.setId(position);
			holder.file_size.setVisibility(View.VISIBLE);
			holder.file_size.setText(PublicTools.changeBKM(String.valueOf(holder.item.length())));
			if (isSelected.size() == 0) {
				getIsSelected();
			}
			holder.checkBox.setChecked(isSelected.get(position));
			holder.checkBox.setOnClickListener(new ViewOnClickListener(this.getItem(position).getPath(), position, 1));
			holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					isSelected.set(buttonView.getId(), isChecked);
				}
			});
		}
		setFileTypeImage(holder, position);
		return convertView;
	}

	private class ViewOnClickListener implements OnClickListener {
		private int index;

		private String path;

		private int viewTypeID;

		public ViewOnClickListener(String path, int index, int viewTypeID) {
			this.index = index;
			this.path = path;
			this.viewTypeID = viewTypeID;
		}

		@Override
		public void onClick(View arg0) {
			if (viewTypeID == 1) {
				if (!((CheckBox) arg0).isChecked()) {
					((CheckBox) arg0).setChecked(false);
					selectedFiles.remove(path);
				} else {
					if (selectedFiles.size() < 10) {

						if (((CheckBox) arg0).isChecked()) {
							if (new File(path).length() > Constant.ONE_GB) {
								Toast.makeText(context, context.getString(R.string.filebrowser_bigger_than_1GB), Toast.LENGTH_SHORT).show();
								((CheckBox) arg0).setChecked(false);
							} else if (new File(path).length() > 0) {
								((CheckBox) arg0).setChecked(true);
								selectedFiles.add(path);
							} else {
								Toast.makeText(context, context.getString(R.string.filebrowser_smaller_than_zero), Toast.LENGTH_SHORT).show();
								((CheckBox) arg0).setChecked(false);
							}
						} else if (!((CheckBox) arg0).isChecked()) {
							((CheckBox) arg0).setChecked(false);
							selectedFiles.remove(path);
						}
					} else {
						((CheckBox) arg0).setChecked(false);
						Toast.makeText(context, context.getResources().getString(R.string.select_file_prompt), Toast.LENGTH_SHORT).show();
					}
				}
			} else if (viewTypeID == 2) {

				if (((CheckBox) arg0).isChecked()) {
					((CheckBox) arg0).setChecked(true);
					selectedFiles.add(path);
				} else {
					((CheckBox) arg0).setChecked(false);
					selectedFiles.remove(files.get(index));
				}

			}
		}
	}

	private void setFileTypeImage(ViewHolder viewHolder, int itemID) {

		String iconPath = DirectoryUtil.generateLocalFileThumbnailPath(context, files.get(itemID).getName());
		viewHolder.iv.setTag(iconPath);
		viewHolder.iv.setImageResource(R.mipmap.default_file_icon);
		if (files.get(itemID).isDirectory()) {
			viewHolder.iv.setImageResource(R.mipmap.folder_im);
		} else {
			if (IconVerification.checkEndsWithInStringArray(files.get(itemID).getName(), context.getResources().getStringArray(R.array.fileEndingImage))) {
				holder.iv.setTag(files.get(itemID).getPath());
				if (mDownloader == null) {
					mDownloader = new ImageDownloader();
				}
				holder.iv.setImageResource(R.mipmap.default_file_icon);
				if (mDownloader != null) {
					mDownloader.imageDownload(files.get(itemID).getPath(), holder.iv, null, 2, "/storage/emulated/0/CloudDrive/thumbnail", context,
							new OnImageDownload() {
								@Override
								public void onDownloadSucc(Bitmap bitmap, String c_url, ImageView mimageView) {
									ImageView imageView = (ImageView) listView.findViewWithTag(c_url);
									if (imageView != null) {
										imageView.setImageBitmap(bitmap);
										imageView.setTag("");
									}
								}
							});
				}
			} else {
				String thumnailPath = DirectoryUtil.generateLocalFileThumbnailPath(context, files.get(itemID).getName());
				IconVerification.fileIconClass(context, listView, viewHolder.iv, 0, files.get(itemID).getName(), false, thumnailPath, null);
			}
		}
	}

	public void clearCacheState() {
		if (null != isSelected) {
			isSelected.clear();
		}
	}

	public void unCheckBox() {
		if (null != holder) {
			holder.checkBox.setChecked(false);
		}
	}

	public class ViewHolder {
		public MyImageView iv;

		public CheckBox checkBox;

		public TextView tv_name;

		public TextView file_size;

		public String name;

		public File item = null;
	}
}
